//
//  HHBezierView.m
//  BezierDemo
//
//  Created by eric on 14-5-7.
//  Copyright (c) 2014年 eric. All rights reserved.
//

#import "HHBezierView.h"
#import "ControlPointView.h"
#define kStartTag 102

@implementation HHBezierView

- (id)init
{
    if (self = [super init])
    {
        bezierData = [[HHBezierData alloc] init];
        [self refreshControlPoint];
    }
    return self;
}

- (void)awakeFromNib
{
    NSLog(@"awakeFromNib");
    bezierData = [[HHBezierData alloc] init];
    [self refreshControlPoint];
    self.backgroundColor = [UIColor clearColor];
    
    UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    addButton.frame = CGRectMake(10,70, 100, 30);
    [addButton setTitle:@"增加控制点" forState:UIControlStateNormal];
    [addButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [addButton addTarget:self action:@selector(addControlPoint) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:addButton];
    
    UIButton *removeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    removeButton.frame = CGRectMake(10,30, 100, 30);
    [removeButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [removeButton setTitle:@"减少控制点" forState:UIControlStateNormal];
    [removeButton addTarget:self action:@selector(removeControlPoint) forControlEvents:UIControlEventTouchUpInside];
     [self addSubview:removeButton];
    
}

- (void)removeControlPoint
{
    [bezierData removeControlPoint];
    [self refreshControlPoint];
    [self setNeedsDisplay];
}

- (void)addControlPoint
{
    [bezierData addControlPoint];
    [self refreshControlPoint];
    [self setNeedsDisplay];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)refreshControlPoint
{
    NSInteger conut = bezierData.interpolantPointsX.rowCount;
    for (UIView *view in self.subviews) {
        if ([view isKindOfClass:[ControlPointView class]])
        {
            [view removeFromSuperview];
        }
    }
    for (int i = 0;i < conut;i++)
    {
        ControlPointView *cView = (ControlPointView *)[self viewWithTag:kStartTag + i];
        if (cView)
        {
            [cView refreshPosition:[self bezierTransformationsToView:CGPointMake(bezierData.interpolantPointsX.data[i][0],bezierData.interpolantPointsY.data[i][0])] controlPoint:CGPointMake(bezierData.interpolantPointsX.data[i][0],bezierData.interpolantPointsY.data[i][0])];
        }
        else
        {
            cView = [[ControlPointView alloc] initWithPosition:[self bezierTransformationsToView:CGPointMake(bezierData.interpolantPointsX.data[i][0],bezierData.interpolantPointsY.data[i][0])] pointNumber:i controlPoint:CGPointMake(bezierData.interpolantPointsX.data[i][0],bezierData.interpolantPointsY.data[i][0])];
            cView.tag = kStartTag + i;
            cView.delegate = (id<MovableViewDelegate>)self;
            [self addSubview:cView];
        }
    }
}


- (void)touchEnded:(UIView *)targeView
{
   CGPoint p =  [self viewTransformationsToBezier:targeView.center];
    bezierData.interpolantPointsX.data[targeView.tag - kStartTag][0] = p.x;
    bezierData.interpolantPointsY.data[targeView.tag - kStartTag][0] = p.y;
    
    [(ControlPointView *)targeView refreshPosition:[self bezierTransformationsToView:CGPointMake(bezierData.interpolantPointsX.data[targeView.tag - kStartTag][0],bezierData.interpolantPointsY.data[targeView.tag - kStartTag][0])] controlPoint:CGPointMake(bezierData.interpolantPointsX.data[targeView.tag - kStartTag][0],bezierData.interpolantPointsY.data[targeView.tag - kStartTag][0])];
    
    [self setNeedsDisplay];
}

- (CGPoint)bezierTransformationsToView:(CGPoint)bPoint
{
    CGFloat screenW = kScreenW;
    CGFloat screenH = kScreenH;
    
    CGFloat x = bPoint.x/kZoom;
    CGFloat y = bPoint.y/kZoom;
    x = x * (screenW/2.0) + screenW/2.0;
    y = (screenH/2.0) - y * (screenH/2.0);
    return CGPointMake(x,y);
}

- (CGPoint)bezierTransformationsToQz:(CGPoint)bPoint
{
    CGFloat screenH = kScreenH;
    
    CGPoint point = [self bezierTransformationsToView:bPoint];
    return CGPointMake(point.x,screenH - point.y);
}

- (CGPoint)viewTransformationsToBezier:(CGPoint)vPoint
{
    CGFloat screenW = kScreenW;
    CGFloat screenH = kScreenH;
    
    CGFloat x = (vPoint.x - (screenW/2.0))/(screenW/2.0);
    CGFloat y = ((screenH/2.0)- vPoint.y)/(screenH/2.0);
    return CGPointMake(x * kZoom, y * kZoom);
}

-(void)drawInContext:(CGContextRef)context
{
    CGContextSetLineWidth(context, 2.0);
    CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 1.0);
    CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
    CGContextMoveToPoint(context, 0.0,kScreenH/2.0);
	CGContextAddLineToPoint(context, kScreenW, kScreenH/2.0);
    CGContextStrokePath(context);
    
    CGContextMoveToPoint(context, kScreenW/2.0,0);
	CGContextAddLineToPoint(context,kScreenW/2.0, 768);
    CGContextStrokePath(context);
	//CGContextFillPath(context);
    
    //CGContextFillRect(context,CGRectMake(0, 0,1000,500));
    
    [[UIColor blueColor] set];
    
    /*
    BZMatrix controlPointsX = BZCreateMatrix(bezierData.interpolantPointsX.rowCount, 1);
	BZMatrix controlPointsY = BZCreateMatrix(bezierData.interpolantPointsY.rowCount, 1);
	
	BZInterpolateCurve(bezierData.interpolantPointsX, bezierData.interpolantPointsY, &controlPointsX, &controlPointsY);
     */
	
	
	BZDrawCurve(bezierData.interpolantPointsX, bezierData.interpolantPointsY, context);
}

- (void)drawRect:(CGRect)rect
{
    [self drawInContext:UIGraphicsGetCurrentContext()];
}

@end
